//
//  LawListCore.m
//  HONGQIYUN
//
//  Created by 余海华 on 2016/12/22.
//  Copyright © 2016年 Haidi. All rights reserved.
//

#import "LawListCore.h"
#import "ILawListRequest.h"
#import "ArticleDetailRequest.h"
#import "CommentListRequest.h"
#import "AddCommentRequest.h"
@interface LawListCore()<ILawList>

@end
@implementation LawListCore
-(void) requestILawListWithCname:(NSString *) cname andCid:(NSString *) cid andFid:(NSString *) fid andPage:(NSString *) page andPageSize:(NSString *) pageSize andKeyWords:(NSString *) keyWords  SuccBlock:(void(^)(ArticlesListInfo *articlesListInfo))succBlock FailBlock:(void(^)(NSString *msg)) failBlock{
    ILawListRequest *api = [[ILawListRequest alloc] initRequestWithCname:cname andCid:cid andFid:fid andPage:page andPageSize:pageSize andKeyWords:keyWords];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToArticlesListInfo:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
    
    
}
-(void) requestArticleDetail:(NSString *) articlId SuccBlock:(void(^)(ArticleObject *articleObject)) succBlock andFailBlock:(void(^)(NSString *msg)) failBlock {
    
    ArticleDetailRequest *api = [[ArticleDetailRequest alloc] initRequestWithId:articlId];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToArticleObject:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
    
}

-(void) requestCommentListWithArticlId:(NSString *)article_id andPage:(NSString *)page andPageSize:(NSString *)pageSize SuccBlock:(void (^)(CommentList *))succBlock FailBlock:(void (^)(NSString *))failBlock {
    
    
    CommentListRequest *api = [[CommentListRequest alloc] initRequestWithArticlId:article_id andPage:page andPageSize:pageSize];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
  
        
        succBlock([self dictionaryToCommentList:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
}

-(void) requestAddCommentWithArticlId:(NSString *) article_id andUserName:(NSString *) userName andContent:(NSString*) content SuccBlock:(void(^)(CommentObject *commentObject)) succBlock FailBlock:(void(^)(NSString *msg)) failBlock {
    
    AddCommentRequest *api = [[AddCommentRequest alloc] initRequestWithArticlId:article_id andUserName:userName andContent:content];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        
        succBlock([self dictionaryToCommentObject:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];

}
-(ArticlesListInfo *) dictionaryToArticlesListInfo:(NSDictionary *) dict {
    
    
    if (dict==nil) {
        return nil;
    }
    
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSArray class]])
    {
 
        [ArticlesListInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"articlesList":@"data",
                     };
            
        }];
 
        [ArticlesListInfo mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"articlesList":[ArticleObject class],
                     };
        }];
        
        [ArticleObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"articleId":@"id",
                     };
            
        }];
        
        [ArticleObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"articleFields":[ArticleFields class],
                     @"articleAlbums":[ArticleAlbums class],
                     @"articleAttachs":[ArticleAttachs class],
                     @"articleGoods":[ArticleGoods class]
                     };
            
            
        }];
 
        
        ArticlesListInfo *list=[ArticlesListInfo mj_objectWithKeyValues:dict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
}

-(ArticleObject *) dictionaryToArticleObject:(NSDictionary*) dict {
    
    if (dict==nil) {
        return nil;
    }
    
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        [ArticlesListInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"articleObject":@"data"
                     };
            
        }];

        [ArticleObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"articleId":@"id",
                     };
            
        }];
        
        [ArticleObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"articleFields":[ArticleFields class],
                     @"articleAlbums":[ArticleAlbums class],
                     @"articleAttachs":[ArticleAttachs class],
                     @"articleGoods":[ArticleGoods class]
                     };
            
            
        }];
        
        
        ArticlesListInfo *list=[ArticlesListInfo mj_objectWithKeyValues:dict];
        return list.articleObject;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
    
}

-(CommentList *) dictionaryToCommentList:(NSDictionary *) dict {
    if (dict==nil) {
        return nil;
    }
    
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *data_dict = [dict valueForKey:@"data"];
        
        [CommentList mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"list":[CommentObject class]
                     };
            
            
        }];
        
        
        CommentList *list=[CommentList mj_objectWithKeyValues:data_dict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
}
-(CommentObject *) dictionaryToCommentObject:(NSDictionary *) dict {
    if (dict==nil) {
        return nil;
    }
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        [CommentList mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            
            return @{
                     @"commentObject":@"data"
                     };
            
            
        }];
    
        CommentList *list=[CommentList mj_objectWithKeyValues:dict];
        return list.commentObject;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
}


@end
